package com.dlc.longweisheng.chongdianxian.pub.net;


import android.support.annotation.Nullable;

import com.dlc.longweisheng.chongdianxian.pub.BaseResponse;
import com.dlc.longweisheng.chongdianxian.pub.PasswordResult;
import com.dlc.longweisheng.chongdianxian.pub.PasswordsResponse;
import com.hoho.android.usbserial.util.LogPlus;
import com.lzy.okgo.model.HttpHeaders;
import com.lzy.okgo.model.HttpParams;

import cn.dlc.commonlibrary.okgo.OkGoWrapper;
import cn.dlc.commonlibrary.okgo.callback.Bean01Callback;
import cn.dlc.commonlibrary.okgo.callback.MyCallback;
import cn.dlc.commonlibrary.okgo.callback.RawBeanCallback;

public class MNet {
    private final OkGoWrapper mOkGoWrapper;

    private MNet() {
        mOkGoWrapper = OkGoWrapper.instance();
    }

    private static class InstanceHolder {
        private static final MNet sInstance = new MNet();
    }

    public static MNet get() {
        return MNet.InstanceHolder.sInstance;
    }


    public <T> void getRequest(String url, @Nullable HttpHeaders headers, @Nullable HttpParams params, Class<T> clazz, MyCallback<T> callback) {
        LogPlus.e("showPrintMsg", "getRequest url:" + url);
        if (params != null) {
            LogPlus.e("showPrintMsg", "getRequest params:" + params.toString());
        }
        mOkGoWrapper.get(url, headers, params, clazz, callback);
    }

    public <T> void postRequest(String url, @Nullable HttpHeaders headers, @Nullable HttpParams params, Class<T> clazz, MyCallback<T> callback) {
        LogPlus.e("showPrintMsg", "postRequest url:" + url);
        if (params != null) {
            LogPlus.e("showPrintMsg", "postRequest params:" + params.toString());
        }
        mOkGoWrapper.post(url, headers, params, clazz, callback);
    }

    public OkGoWrapper getmOkGoWrapper() {
        return mOkGoWrapper;
    }

    public void getPassword(String macno, Bean01Callback<PasswordResult> callback) {
        HttpParams params = new HttpParams();
        params.put("macno", macno);
        postRequest(Urls.BASE_URL + Urls.GENERATE, null, params, PasswordResult.class, callback);
    }

    public void getOldPassword(String macno, Bean01Callback<PasswordResult> callback) {
        HttpParams params = new HttpParams();
        params.put("macno", macno);
        postRequest(Urls.BASE_URL + Urls.GET_CODE_ARRAY, null, params, PasswordResult.class, callback);
    }

    public void getPassword2(String chargerId, String pwd, RawBeanCallback<PasswordsResponse> callback) {
        HttpParams params = new HttpParams();
        params.put("chargerId", chargerId);
        params.put("pwd", pwd);
        postRequest(Urls.BASE_URL + Urls.PASSWORDS, null, params, PasswordsResponse.class, callback);
    }

    public void requestPasswordApplied(String chargerId, String factorsIdx, String chargerPwds, RawBeanCallback<BaseResponse> callback) {
        HttpParams params = new HttpParams();
        params.put("chargerId", chargerId);
        params.put("factorsIdx", factorsIdx);
        params.put("chargerPwds", chargerPwds);
        postRequest(Urls.BASE_URL + Urls.PASSWORDS_APPLY, null, params, BaseResponse.class, callback);
    }
}
